﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CoolCode.Web.UI.Controls
{
    public partial class NumericPager : System.Web.UI.UserControl,IPager
    {
        public event GridViewPageEventHandler PageIndexChanging;

        public int PageIndex { get { return (int)(ViewState["PageIndex"] ?? 0); } set { ViewState["PageIndex"] = value; } }
        public int PageSize { get { return (int)(ViewState["PageSize"] ?? 10); } set { ViewState["PageSize"] = value; } }
        public int PageCount { get { return (int)(ViewState["PageCount"] ?? 1); } set { ViewState["PageCount"] = value; } }
        //public int TotalRecords { get { return (int)(ViewState["TotalRecords"] ?? 0); } set { ViewState["TotalRecords"] = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                /*For binding events*/
                dynamicCreatePager(PageIndex, PageCount);
            }
        }

        public virtual void Bind(IPaginatedList p)
        {
            this.PageIndex = p.PageIndex;
            this.PageCount = p.PageCount;
            this.PageSize = p.PageSize;
            //this.TotalRecords = p.TotalRecords;
            if (p.TotalRecords == 0)
            {
                this.Visible = false;
                return;
            }
            else
            {
                this.Visible = true;
            }
            dynamicCreatePager(p.PageIndex, p.PageCount);
            ltrPageCount.Text = this.PageCount.ToString();
            lblDspTotalRecords.Text = p.TotalRecords.ToString();
        }

        protected void PagerButton_Click(object sender, EventArgs e)
        {
            string arg = ((IButtonControl)sender).CommandArgument;
            int pageIndex;
            if (int.TryParse(arg, out pageIndex))
            {
                this.PageIndex = pageIndex;
            }
            PageIndexChanging(this, new GridViewPageEventArgs(this.PageIndex));
        }

        private void dynamicCreatePager(int pageIndex, int pageCount)
        {  
            pagerHolder.Controls.Clear();
            if (pageCount <= 1) return;
            for (int i = 0; i < pageCount; i++)
            {
                if (i != pageIndex)
                {
                    var pageButton = new LinkButton
                    {
                        ID = "PageNumber" + i,
                        CommandArgument = i.ToString(),
                        Text = (i + 1).ToString(),
                    };
                    pageButton.Click += PagerButton_Click;
                    pagerHolder.Controls.Add(pageButton);
                }
                else
                {
                    var pageLabel = new Label
                    {
                        ID = "PageNumber" + i,
                        Text = (i + 1).ToString(),
                    };
                    pagerHolder.Controls.Add(pageLabel);
                }
                pagerHolder.Controls.Add(new Label { Width = 20 });
            }
        }
    }
}